# Dinghong Cloud Screen Project Docker Image Build Script (Windows PowerShell)
# For Minikube Deployment

param(
    [switch]$SkipBuild
)

$ErrorActionPreference = "Stop"

Write-Host "Starting to build Dinghong Cloud Screen project Docker images..." -ForegroundColor Green

# Get project root directory - Method 2 (3 levels up) is correct
$PROJECT_ROOT = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path))
Write-Host "Project root directory: $PROJECT_ROOT" -ForegroundColor Cyan

# Verify the path is correct
$serverPath = Join-Path $PROJECT_ROOT "server"
$webPath = Join-Path $PROJECT_ROOT "web"

if (-not (Test-Path $serverPath)) {
    Write-Host "ERROR: Server directory not found at: $serverPath" -ForegroundColor Red
    exit 1
}

if (-not (Test-Path $webPath)) {
    Write-Host "ERROR: Web directory not found at: $webPath" -ForegroundColor Red
    exit 1
}

Write-Host "✓ Server directory found at: $serverPath" -ForegroundColor Green
Write-Host "✓ Web directory found at: $webPath" -ForegroundColor Green

# Build backend image
Write-Host "Building backend image..." -ForegroundColor Yellow
Set-Location $serverPath
docker build -t dinghong-cloud-screen-server:latest .

# Build frontend image
Write-Host "Building frontend image..." -ForegroundColor Yellow
Set-Location $webPath
docker build -t dinghong-cloud-screen-web:latest .

# Load images to Minikube
Write-Host "Loading images to Minikube..." -ForegroundColor Yellow
minikube image load dinghong-cloud-screen-server:latest
minikube image load dinghong-cloud-screen-web:latest

Write-Host "Image build completed!" -ForegroundColor Green
Write-Host "Backend image: dinghong-cloud-screen-server:latest" -ForegroundColor Cyan
Write-Host "Frontend image: dinghong-cloud-screen-web:latest" -ForegroundColor Cyan 